package com.ms.education.course.controller.front;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ms.education.JwtUtils;
import com.ms.education.R;
import com.ms.education.course.entity.Course;
import com.ms.education.course.service.CourseService;
import com.ms.education.exceptionHandler.EduException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>Project:education-IndexFrontController
 * <p>Powered by ms on 2023-02-12 19:59:40
 *
 * @author ms
 * @version 1.0
 * @since 1.8
 */
@RestController
@RequestMapping("/course/front/course")
@Api(description = "前台课程信息")
public class IndexCourseController {

    @Autowired
    private CourseService courseService;

    @ApiOperation("前8个热门课程")
    @GetMapping("index")
    @Cacheable(value = "frontCourse")
    public R index() {
        List<Course> courses = courseService.index();
        return R.ok().data("courses", courses);
    }

    @ApiOperation("查询讲师相关的课程")
    @GetMapping("getTeacherCourses/{teacherId}")
    public R getTeacherCourses(@PathVariable("teacherId") Integer teacherId) {
        List<Course> courses = courseService.getTeacherCourses(teacherId);
        return R.ok().data("courses", courses);
    }

    @SaCheckLogin
    @ApiOperation("查询用户收集的视频")
    @GetMapping("pageCourseOfUser/{current}/{limit}")
    public R pageCourseOfUser(@PathVariable("current") long current,
                              @PathVariable("limit") long limit) {
        Page<Course> page = courseService.pageCourseOfUser(current, limit, Convert.toInt(StpUtil.getLoginId()));
        long rows = page.getTotal();
        List<Course> courses = page.getRecords();
        return R.ok().data("rows", rows).data("courses", courses);
    }

}
